package ex;

public class Complex {
    // 定义属性并生成getter,setter
    double RealPart;
    double ImagePart;

    public double getRealPart() {
        return RealPart;
    }

    public double getImagePart() {
        return ImagePart;
    }

    public void setRealPart(double realPart) {
        RealPart = realPart;
    }

    public void setImagePart(double imagePart) {
        ImagePart = imagePart;
    }

    // 定义构造函数
    public Complex(double R, double I)
    {
        RealPart = R;
        ImagePart = I;
    }

    //Override Object
    public boolean equals(Complex obj){
        if (this.getRealPart() == obj.getRealPart() && this.getImagePart() == obj.getImagePart())
            return true;
        else
            return false;
    }
    public String toString(){
        String result;
        if (ImagePart >= 0)
            if (ImagePart > 0)
            result = RealPart + "+" + ImagePart + "i";
            else
                result = RealPart + "";
        else
            result = RealPart + "" +  ImagePart + "i";

        return result;
    }

    // 定义公有方法:加减乘除
    // 复数加法
    Complex ComplexAdd(Complex a)
    {
        Complex result;
        double real, image;
        real = RealPart + a.getRealPart();
        image = ImagePart + a.getImagePart();
        return result = new Complex(real, image);
    }

    // 复数减法
    Complex ComplexSub(Complex a)
    {
        Complex result;
        double real, image;
        real = RealPart - a.getRealPart();
        image = ImagePart - a.getImagePart();
        return result = new Complex(real, image);
    }

    // 复数乘法
    Complex ComplexMulti(Complex a)
    {
        Complex result;
        double real, image;
        real = RealPart * a.getRealPart() - ImagePart * a.getImagePart();
        image = RealPart * a.getImagePart() + ImagePart * a.getRealPart();
        return result = new Complex(real, image);
    }

    // 复数除法
    Complex ComplexDiv(Complex a)
    {
        Complex result;
        double real, image;
        real =( RealPart * a.getRealPart() - ImagePart * a.getImagePart()) / (RealPart + Math.pow(ImagePart, 2));
        image =( RealPart * a.getImagePart() + ImagePart * a.getRealPart()) / (RealPart + Math.pow(ImagePart, 2 ));
        return result = new Complex(real, image);
    }
}
